package com.application.metroscanner.adapters;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.application.metroscanner.tools.Classifier;
import com.example.metroscanner.R;

public class ClustersListAdapter extends ArrayAdapter<Classifier.Cluster> {

	private final Context context;
	private final ArrayList<Classifier.Cluster> values;
	private static NumberFormat hourFormatter = new DecimalFormat("##");

	public ClustersListAdapter(Context context, ArrayList<Classifier.Cluster> clusters) {
		super(context, R.layout.view_ride_groups_list, clusters);
		this.context = context;
		this.values = clusters;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		View rowView = convertView;
		if (rowView == null) {
			rowView = inflater.inflate(R.layout.view_ride_groups_list, parent,
					false);
		}

		Classifier.Cluster group = (Classifier.Cluster) values.get(position);
		
		String startTime = hourFormatter.format(group.getFirstRide().getHours())+"h"+hourFormatter.format(group.getFirstRide().getMinutes())+"mn";				
		TextView startLine = (TextView) rowView.findViewById(R.id.start);
		startLine.setText(startTime);
		
		String finishTime = hourFormatter.format(group.getLastRide().getHours())+"h"+hourFormatter.format(group.getLastRide().getMinutes())+"mn";
		TextView finishLine = (TextView) rowView.findViewById(R.id.finish);
		finishLine.setText(finishTime);
		
		return rowView;
	}
}
